#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

#ifdef QT_NO_DEBUG
    ui->menubar->hide();
#endif
    this->setWindowTitle(ui->title->text());
    this->resize(1280,720+200);
    this->setMaximumSize(1280,720+200);


    PAGEJUMP->setConterWgt(ui->subWgt);
    PAGEJUMP->setLayout(ui->subWgtLayout);

    PAGEJUMP->setPage(DK_Log);
    ui->topMenu->hide();
    ui->logoutBtn->hide();
    ui->userLabel->setText("未登录");


    connect(&GDATA->signalObj,&SignalObj::sigLoginStatusChanged,[=](){
        if(GDATA->isLogSuccess){
            QString info = "%1 : %2";
            info = info.arg(UserTypeStrChinese[GDATA->userType]).arg(GDATA->name);
            ui->userLabel->setText(info);

            start(GDATA->userType);
        }else{
            ui->userLabel->setText("未登录");
        }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::start(int i)
{
    ui->logoutBtn->show();

    if(i == 0){
        ui->topMenu->show();
    }else{
        ui->topMenu->hide();
    }

    switch (i) {
        case  0 : {
            startAdmin();
            break;
        }
        case  1 : {

            startCounselor();
            break;
        }
        case  2 : {
            startTeacher();
            break;
        }
        case  3 : {
            startStudent();
            break;
        }
    }
}

void MainWindow::startAdmin()
{
    PAGEJUMP->setPage(DK_AD_StudentMgrList);
}
#include "ad_oneclassinfo.h"
#include <QTableView>
void MainWindow::startCounselor()
{
//    PAGEJUMP->setPage(DK_TE_ClassGrade);
//    return ;

    PAGEJUMP->setPage(DK_AD_OneClassInfo);
    auto wgt = qobject_cast<AD_OneClassInfo*>(PAGEJUMP->getPage(DK_AD_OneClassInfo));

    if(!wgt){
        return;
    }



    wgt->setClassListComboBox(QString("consultant_id = %1").arg(GDATA->Account));
    wgt->setMode("update_consultant");
}

void MainWindow::startTeacher()
{
    PAGEJUMP->setPage(DK_TE_ClassGrade);
}

void MainWindow::startStudent()
{
    PAGEJUMP->setPage(DK_ST_OneStudentInfo);
}

void MainWindow::logOut()
{
    PAGEJUMP->setPage(DK_Log);
    ui->topMenu->hide();
    ui->logoutBtn->hide();
    ui->userLabel->setText("未登录");
}



void MainWindow::on_actionsql_triggered()
{
    DB->test();
}

void MainWindow::on_logoutBtn_clicked()
{
    logOut();
}


#include "qtcsv/stringdata.h"
#include "qtcsv/reader.h"
#include "qtcsv/writer.h"

#include <QDir>

void MainWindow::on_actioncsv_triggered()
{
    // prepare data that you want to save to csv-file
    QStringList strList;
    strList << "one" << "two" << "three";

    QtCSV::StringData strData;
    strData.addRow(strList);
    strData.addEmptyRow();
    strData << strList << "this is the last row";

    // write to file
    QString filePath = QDir::currentPath() + "/test.csv";
    QtCSV::Writer::write(filePath, strData);

    // read data from file
    QList<QStringList> readData = QtCSV::Reader::readToList(filePath);
    for (int i = 0; i < readData.size(); ++i)
    {
        qDebug() << readData.at(i).join(",");
    }
}


void MainWindow::on_actionshowErr_triggered()
{
    TOOLS->showErr("测试");
}

